package com.ubs.lingua.buildtool.cs.utils.ant;

import java.io.File;

import org.apache.commons.logging.LogFactory;
import org.apache.tools.ant.Project;
import org.apache.tools.ant.Target;
import org.apache.tools.ant.taskdefs.Expand;

/**
 * Wrapper for the Apache Ant UNZIP task.
 */
public final class Expander extends Expand {

    /**
     * Private constructor which setup the mock Ant project and adds the {@link AntLogListener} to it
     */
    private Expander() {
        super();

        // creating mock project
        final Project prj = new Project();
        prj.setName("Mock Expander project");
        prj.addBuildListener(new AntLogListener(LogFactory.getLog(Expander.class)));
        setProject(prj);

        setTaskType("unzip");
        setTaskName("unzip");

        // creating mock target
        final Target target = new Target();
        target.setName("Mock Unzip target");
        setOwningTarget(target);
    }

    /**
     * Unzips a zip-, war-, or jar file.
     * @param zipFile archive file to expand
     * @param destinationDir directory where to store the expanded files
     */
    public static void antUnzip(final File zipFile, final File destinationDir) {
        final Expander expander = new Expander();
        expander.setSrc(zipFile);
        expander.setDest(destinationDir);

        AntLauncher.launchTask(expander);
    }

}
